package com.sztx.pay.center.worker.quartz;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.sztx.pay.center.common.util.ConfigUtil;
import com.sztx.pay.center.common.util.Constance.UserActType;
import com.sztx.pay.center.common.util.DateUtil;
import com.sztx.pay.center.core.service.AccountService;
import com.sztx.pay.center.dataaccess.dao.mysql.UserAmountCollectMysqlDAO;
import com.sztx.pay.center.dataaccess.domain.UserActAmountDO;
import com.sztx.pay.center.dataaccess.domain.UserAmountCollectDO;
import com.sztx.se.core.quartz.job.BaseJob;

@Component("collectUserAmountJob")
public class CollectUserAmountJob extends BaseJob{

	@Autowired
	UserAmountCollectMysqlDAO userAmountCollectMysqlDAO;
	@Autowired
	AccountService accountService;
	private final Logger LOG = LoggerFactory.getLogger(CollectUserAmountJob.class);
	@Override
	public Object executeJob(JobExecutionContext context)
			throws JobExecutionException {
		LOG.info("---------collectUserAmountJob start---------");
		Date collectDate = getCollectDate();
		
		String collectDateStr = DateUtil.formatDate(collectDate, DateUtil.SHORT_DATE_FORMAT_STR);
		
		List<UserAmountCollectDO> collectDOList = userAmountCollectMysqlDAO.listPage(collectDateStr, collectDateStr, null);
		if(collectDOList != null && collectDOList.size() > 0){
			LOG.info("统计{}的数据已经存在,不需要重复统计",collectDateStr);
			return null;
		}
		UserAmountCollectDO userAmountCollectDO = new UserAmountCollectDO();
		
		List<String> includeUserList = new ArrayList<String>();
		String collectIncludeUsers = ConfigUtil.getCollectIncludeUsers();
		if(StringUtils.isNotBlank(collectIncludeUsers)){
			includeUserList = Arrays.asList(collectIncludeUsers.split(","));
		}
		
		List<UserActAmountDO> userActAmountDOList = accountService.queryUserAmountCollect(includeUserList);
		
		LOG.info("---userActAmountDOList--"+JSONObject.toJSONString(userActAmountDOList));
		Integer userNum = accountService.queryCollectAmountUserCount(includeUserList);
		userAmountCollectDO.setCollectDate(collectDate);
		userAmountCollectDO.setUserNum(userNum);
		for (UserActAmountDO userActAmountDO : userActAmountDOList) {
			UserActType userActType = UserActType.valueOf(userActAmountDO.getActType());
			switch (userActType) {
			case CASH:
				userAmountCollectDO.setCashActCollectAmount(userActAmountDO.getActCollectAmount());
				userAmountCollectDO.setCashActFreezeCollectAmount(userActAmountDO.getActFreezeCollectAmount());
				userAmountCollectDO.setCashActNum(userActAmountDO.getActNum());
				break;
			case TRADE:
				userAmountCollectDO.setTradeActCollectAmount(userActAmountDO.getActCollectAmount());
				userAmountCollectDO.setTradeActFreezeCollectAmount(userActAmountDO.getActFreezeCollectAmount());
				userAmountCollectDO.setTradeActNum(userActAmountDO.getActNum());			
				break;
			case JF:
				userAmountCollectDO.setJfActCollectAmount(userActAmountDO.getActCollectAmount());
				userAmountCollectDO.setJfActFreezeCollectAmount(userActAmountDO.getActFreezeCollectAmount());
				userAmountCollectDO.setJfActNum(userActAmountDO.getActNum());
				break;

			default:
				break;
			}
		}
		userAmountCollectMysqlDAO.save(userAmountCollectDO);
		
		
		LOG.info("---------collectUserAmountJob end---------");
		return null;
	}
	
	
	private Date getCollectDate(){
		Calendar ca = Calendar.getInstance();
		ca.setTime(new Date());
		ca.add(Calendar.DAY_OF_YEAR, -1);
		Date collectDate = ca.getTime();
		return collectDate;
	}
	
}
